// 按受欢迎程度提升权重: 希望点赞多的blog, 放在搜索列表相对靠前的文字, 同时搜索的评分, 还是作为主要的依据
// 新的算分 = 老的算分 * 投票数: 但是投票数为0或者很大的时候
DELETE blogs

PUT /blogs/_doc/1
{
  "title":   "About popularity",
  "content": "In this post we will talk about...",
  "votes":   0
}

PUT /blogs/_doc/2
{
  "title":   "About popularity",
  "content": "In this post we will talk about...",
  "votes":   100
}

PUT /blogs/_doc/3
{
  "title":   "About popularity",
  "content": "In this post we will talk about...",
  "votes":   1000000
}

// title, content, 完全一样, votes不一样, 查询结果差异性很大, votes为0的, 分数直接变为了0
POST /blogs/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query":    "popularity",
          "fields": [ "title", "content" ]
        }
      },
      "field_value_factor": {
        "field": "votes"
      }
    }
  }
}

// 使用modifier log1p: 新的算分 = 老的算分 * log(1 + votes)
// 其他的modifier: none, log, log1p, log2p, ln, ln1p, ln2p, square, sqrt, reciprocal
POST /blogs/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query":    "popularity",
          "fields": [ "title", "content" ]
        }
      },
      "field_value_factor": {
        "field": "votes",
        "modifier": "log1p"
      }
    }
  }
}

// 引入factor: 新的算分 = 老的算分 * log(1 + factor * votes)
// 更改曲线的曲率
POST /blogs/_search
{
  "query": {
    "function_score": {
      "query": {
        "multi_match": {
          "query":    "popularity",
          "fields": [ "title", "content" ]
        }
      },
      "field_value_factor": {
        "field": "votes",
        "modifier": "log1p",
        "factor": 0.1
      }
    }
  }
}

// function_score 的 boost Mode : 
// - Multiply: 算分和函数值的乘积(默认)
// - sum: 算分和函数值 相加
// - min/max: 算分和函数值 取 最小/大 值
// - replace: 使用函数值取代算分

// 还有一个 max_boost 可以控制算分控制在一个最大值

// 新的算分 = 老的算分 + log(1 + factor * votes)
POST /blogs/_search
{
    "query": {
        "function_score": {
            "boost_mode": "sum",
            "query": {
                "multi_match": {
                    "query": "popularity",
                    "fields": [
                        "title",
                        "content"
                    ]
                }
            },
            "field_value_factor": {
                "field": "votes",
                "modifier": "log1p",
                "factor": 0.1
            }
        }
    }
}

// max_boost
POST /blogs/_search
{
    "query": {
        "function_score": {
            "boost_mode": "sum",
            "max_boost": 3,
            "query": {
                "multi_match": {
                    "query": "popularity",
                    "fields": [
                        "title",
                        "content"
                    ]
                }
            },
            "field_value_factor": {
                "field": "votes",
                "modifier": "log1p",
                "factor": 0.1
            }
        }
    }
}


//function_score.random_score
// 网站的广告需要提高展现率: 让每个用户看到不同的随机排名, 但是同一用户访问时, 结果是相对的, 保持一致性

GET /blogs/_search
{
    "query": {
        "function_score": {
            "random_score": {
                "seed": 911119 // 只要种子一致, 顺序就一致, 比如用uid作为种子
            }
        }
    }
}
